package com.lsq.tucao.user.controller;

import com.lsq.tucao.user.pojo.po.User;
import com.lsq.tucao.user.response.JsonResult;
import com.lsq.tucao.user.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * @author lsq
     * @description 分页获取所有的用户信息
     * @param current 页码
     * @param size 每页条数
     * @return com.lsq.tucao.user.response.JsonResult
     * @create 2021/4/16 23:14
     */
    @GetMapping("/page")
    public JsonResult pageUser(@RequestParam(required = false,defaultValue = "1") Integer current,
                               @RequestParam(required = false,defaultValue = "10") Integer size){
        log.debug("current:{}",current);
        log.debug("size:{}",size);

        return userService.pageUser(current, size);
    }

    /**
     * @author lsq
     * @description 根据用户名获取用户信息
     * @param username 用户名
     * @return com.lsq.tucao.user.response.JsonResult
     * @create 2021/4/17 20:30
     */
    @GetMapping("/one")
    public JsonResult getOneUserByName(@RequestParam String username){
        log.debug("username:{}",username);

        return userService.getUserByName(username);
    }


    @GetMapping("/id")
    public JsonResult detail(@RequestParam Long userId) {
        User user = userService.getById(userId);
        if(user != null){
            return new JsonResult(user,200,"查询用户成功");
        }
        return new JsonResult(500,"查询用户失败");
    }

    /**
     * @author lsq
     * @description 根据用户名模糊查询用户
     * @param username 用户名
     * @return com.lsq.tucao.user.response.JsonResult
     * @create 2021/4/19 22:28
     */
    @GetMapping("/like")
    public JsonResult searchUser(@RequestParam(required = false,defaultValue = "1") Integer current,
                                 @RequestParam(required = false,defaultValue = "10") Integer size,
                                 @RequestParam String username){
        log.debug("current:{}",current);
        log.debug("size:{}",size);
        log.debug("username:{}",username);

        return userService.searchUser(current,size,username);
    }

    /**
     * 查看用户的具体信息
     */
    @GetMapping("/detail")
    public JsonResult detail(@RequestParam Integer userId) {
        User user = userService.getById(userId);
        JsonResult result = userService.getUserDetailByName(user.getUsername());

        return result;
    }

    /**
     * @author lsq
     * @description 添加用户
     * @param user 用户实体类
     * @return com.lsq.tucao.user.response.JsonResult
     * @create 2021/4/17 10:22
     */
    @PostMapping
    public JsonResult addUser(@RequestBody User user){
        log.debug("user:{}",user);

        return userService.addUser(user);
    }

    /**
     * @author lsq
     * @description 根据yonghuid删除用户信息并且删除用户角色关联表中的相应信息
     * @param userId 用户id
     * @return com.lsq.tucao.user.response.JsonResult
     * @create 2021/4/18 15:54
     */
    @DeleteMapping
    public JsonResult deleteUser(@RequestParam Long userId) {
        return userService.deleteUserById(userId);
    }

    /**
     * @author lsq
     * @description 修改用户信息
     * @param user 修改后的用户实体类
     * @param isUpdateUsername 是否修改了用户名
     * @return com.lsq.tucao.user.response.JsonResult
     * @create 2021/4/18 16:07
     */
    @PatchMapping
    public JsonResult updateUser(@RequestBody User user,@RequestParam Boolean isUpdateUsername) {
        log.debug("user:{}",user);
        log.debug("isUpdateUsername:{}",isUpdateUsername);

        return userService.updateUser(user,isUpdateUsername);
    }

    /**
     * @author lsq
     * @description 修改管理员的登录密码
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return com.lsq.tucao.user.response.JsonResult
     * @create 2021/4/17 21:40
     */
    @PatchMapping("/admin/password")
    public JsonResult updateAdmin(@RequestParam String oldPassword,
                                  @RequestParam String newPassword){
        return userService.updateAdminPassword(oldPassword,newPassword);
    }


}

